package com.cloudbroker.bcs.csvc.lightinvesting.facade.service.aspect;

import com.cloudbroker.bcs.platform.ltb.bizapi.constants.type.UserCacheFreshType;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.base.vo.BrokerVO;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.base.vo.UserVO;
import com.cloudbroker.bcs.platform.ltb.common.config.ConfigValues;
import com.cloudbroker.bcs.platform.ltb.common.util.CacheHelper;
import com.cloudbroker.bcs.platform.ltb.common.util.SessionHelper;
import com.hundsun.jres.common.util.UUID;

/**
 * Created by tianrui on 2016/7/8.
 * user_token 缓存数据 userInfo + user_key + op_entrust_way + op_station + terminal_type
 */
public class UserTokenUtil {

    /**
     * 新增user_token缓存
     * */
    public static String addUserToken(UserCache userCache){
        String user_token = UUID.randomUUID().toString();
        SessionHelper sessionHelper = SessionHelper.getInstance();
//        String user_token = sessionHelper.getAccess_tokenKey(user_key);
        sessionHelper.setInfoForSession(user_token, userCache.toStringInfo(),
                ConfigValues.getSessionTimeout());
        return user_token;
    }

    /**
     * 更新user_token缓存
     * */
    public static void updateUserToken(String user_token, UserCache userCache){
        SessionHelper sessionHelper = SessionHelper.getInstance();
        sessionHelper.setInfoForSession(user_token, userCache.toStringInfo());
    }

    /**
     * 查询user_token缓存
     * */
    public static UserCache queryUserToken(String user_token){
        CacheHelper cacheHelper = CacheHelper.getInstance();
        return UserCache.fromStringInfo(cacheHelper.get(user_token));
    }


    /**
     * 删除user_token缓存
     * */
    public static void deleteUserToken(String user_token){
        SessionHelper.getInstance().delete(user_token);
    }


    /**
     * 用户缓存密码校验
     * */
    public static boolean isRightOldPassword(String user_token, String oldPassword){
        UserCache userCache = queryUserToken(user_token);
        if (null == userCache){
            return false;
        }
        UserVO user = userCache.getBrokerVO().getUser();
        if (null == user){
            return false;
        }
        if (oldPassword.equals(user.getPassword())){
            return true;
        }
        return false;
    }

    /**
     * 用户密码修改后缓存刷新
     * */
    public static <T> void updateByUpdateKey(String user_token, T update_key, UserCacheFreshType freshType){
        UserCache userCache = queryUserToken(user_token);
        if (null != userCache){
            switch (freshType){
                case UPDATE_PASSWORD:
//                    userCache.getBrokerVO().getUser().setPassword((String)update_key);
                    break;
                case UPDATE_AVATAR: {
//                    Customer customer = (Customer) userCache.getBrokerVO().getUser();
//                    customer.setAvatar(String.valueOf(update_key));
//                    userCache.getBrokerVO().setUser(customer);
                }
                    break;
                case UPDATE_ALIAS:
//                    Customer customer = (Customer) userCache.getBrokerVO().getUser();
//                    customer.setAlias_name((String)update_key);
//                    userCache.getBrokerVO().setUser(customer);
                    break;
            }
        }
        updateUserToken(user_token, userCache);
    }

    /**
     * 达人简介信息修改时间校验 若存在则不允许修改
     * */
    public static boolean leaderBackgroundEditVerify(String leader_id, String company_id){
        String user_token = "leaderBackgroundEditVerify" + leader_id + company_id;
        if (null != queryUserToken(user_token)){
            return false;
        }
        UserCache userCache = new UserCache();
        BrokerVO brokerVO = new BrokerVO();
        brokerVO.setCompany_id(company_id);
        userCache.setBrokerVO(brokerVO);
        SessionHelper sessionHelper = SessionHelper.getInstance();
        sessionHelper.setInfoForSession(user_token, userCache.toStringInfo(), ConfigValues.getLeaderUpdateTime());
        return true;
    }

}
